{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}

module Main where

import GHC.Generics (Generic)
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Data.ByteString.Lazy as BL
import Data.Aeson (encode, ToJSON)
import Data.Maybe (mapMaybe)
import Text.Read (readMaybe)

-- 定义记录类型
data Person = Person
  { name :: T.Text
  , age :: Int
  , email :: T.Text
  } deriving (Show, Generic)  

instance ToJSON Person  

-- 解析单行文本
parseLine :: T.Text -> Maybe Person
parseLine line = case T.splitOn "|" (T.strip line) of
  [name, ageStr, email] -> do
    age <- readMaybe (T.unpack ageStr)  
    Just $ Person name age email
  _ -> Nothing  

-- 主函数
main :: IO ()
main = do
  -- 1. 读取文件
  content <- TIO.readFile "src/input.txt"
  
  -- 2. 处理数据
  let people = mapMaybe parseLine (T.lines content)
  
  -- 3. 写入JSON
  BL.writeFile "output.json" (encode people)
  putStrLn "OK"